package com.tuoyu.peopletest.view.custom;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;

import com.tuoyu.peopletest.R;
import com.tuoyu.peopletest.utils.SCLogUtils;

import androidx.annotation.Nullable;

/**
 *
 */
public class MyTestProgressView extends View {
    private final static String TAG = "MyTestProgressView";
    private Rect rectOut;
    private final Paint mPaintOut;
    private final Paint mPaintBg;
    private Rect rectBg;
    private final Paint mPaintProgress;
    private Rect rectProgress;

    public MyTestProgressView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        int colorOut = getResources().getColor(R.color.sc_c_bg_gray_40_60);
        int colorBg = getResources().getColor(R.color.sc_c_bg_white);
        int colorProgress = getResources().getColor(R.color.sc_c_bg_green);

        mPaintOut = new Paint();
        mPaintOut.setDither(true);
        mPaintOut.setAntiAlias(true);
        mPaintOut.setColor(colorOut);
        mPaintOut.setStyle(Paint.Style.STROKE);
        mPaintOut.setStrokeWidth(dip2px(1));

        mPaintBg = new Paint();
        mPaintBg.setDither(true);
        mPaintBg.setAntiAlias(true);
        mPaintBg.setColor(colorBg);
        mPaintBg.setStyle(Paint.Style.FILL);

        mPaintProgress = new Paint();
        mPaintProgress.setDither(true);
        mPaintProgress.setAntiAlias(true);
        mPaintProgress.setColor(colorProgress);
        mPaintProgress.setStyle(Paint.Style.FILL);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        int height = getHeight();
        SCLogUtils.e(TAG,"布局高度="+height);
        height = (int) (getHeight() * 1f / 2);
        SCLogUtils.e(TAG,"布局高度 ="+height);
        //外边框
        rectOut = new Rect();
        rectOut.set(0, 0, getWidth(), height);

        //背景
        rectBg = new Rect();
        rectBg.set(0, 0, getWidth(), height);

        //进度条
        rectProgress = new Rect();
        rectProgress.set(0, 0, (int) (getWidth() * 1f / 2), height);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (rectBg == null) {
            return;
        }
        canvas.drawRect(rectBg, mPaintBg);
        canvas.drawRect(rectProgress, mPaintProgress);
        canvas.drawRect(rectOut, mPaintOut);
    }

    public int dip2px(float dpValue) {
        final float scale = getContext().getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    public int sp2px(float spValue) {
        final float fontScale = getContext().getResources().getDisplayMetrics().scaledDensity;
        return (int) (spValue * fontScale + 0.5f);
    }
}
